package com.sky.course.controller;

import com.sky.course.entity.Course;
import com.sky.course.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Description:    课程服务端
* @Author:         Joe
* @CreateDate:     2019/10/24 11:39
*/
@RestController
@RequestMapping(value = "/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public boolean add(@RequestBody Course course){
        return courseService.add(course);
    }

    @RequestMapping(value = "/get/{cid}",method = RequestMethod.GET)
    public Course get(@PathVariable("cid")Integer cid){
        return courseService.get(cid);
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Course> list(){
        return courseService.list();
    }

    @RequestMapping(value = "/discovery",method = RequestMethod.GET)
    public Object discovery(){

        /*获取eureka中的服务*/
        List<String> list = client.getServices();
        System.out.println("对应的服务有："+list);
        List<ServiceInstance> srvList = client.getInstances("COURSE");
        for (ServiceInstance instance:srvList){
            System.out.println(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }
        return this.client;
    }
}
